package com.akn.mvvmlibrary.activity

import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import com.akn.mvvmlibrary.R
import com.blankj.utilcode.util.ActivityUtils
import com.akn.mvvmlibrary.factory.AppViewModelFactory
import com.akn.mvvmlibrary.viewmodel.BaseViewModel
import com.qmuiteam.qmui.util.QMUIStatusBarHelper

abstract class BaseActivity<VDB : ViewDataBinding, VM : BaseViewModel> : AppCompatActivity() {

    lateinit var binding: VDB

    lateinit var viewModel: VM


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStatusBar()
        val vm = createViewModel()
        viewModel = ViewModelProvider(
            this,
            AppViewModelFactory(vm)
        ).get(vm::class.java)
        buildViewDataBinding(getLayoutId())
        val ivRight = findViewById<ImageView>(R.id.ivRight)
        ivRight?.setImageResource(viewModel.imageRightResId())
        initView()
    }

    open fun setStatusBar() {
        QMUIStatusBarHelper.translucent(this)
        QMUIStatusBarHelper.setStatusBarLightMode(this)
    }

    /*构建一个DataBindingView*/
    private fun buildViewDataBinding(layoutId: Int) {
        binding = DataBindingUtil.setContentView(ActivityUtils.getTopActivity(), layoutId)
        binding.setVariable(variableId(), viewModel)
        binding.lifecycleOwner = this
    }

    /*子类实现，返回一个布局id*/
    abstract fun getLayoutId(): Int

    abstract fun initView()

    /*子类实现，返回一个viewModel*/
    abstract fun createViewModel(): VM

    /*需要绑定的viewModel的BR.id*/
    abstract fun variableId(): Int


}